<template>
	<view class="page chat-box">
		<view class="header" :style="{ paddingTop: navBarHeight }">
			<view class="btn-side left" @click="onNavBack()">
				<image src="../../static/img/chat-09.png"></image>
			</view>

			<text class="title">{{title}}</text>
			<view class="btn-side right" @click="onShowMore()">
				<image src="../../static/img/chat-10.png"></image>
			</view>

		</view>
		
		
		<view class="chat-msg" @click="switchChatTabBox('none',true)">
			<scroll-view class="scroll-box" scroll-y="true" @scrolltoupper="onScrollToTop"
				:scroll-into-view="'chat-item-'+scrollMsgIdx" @scroll="handleScroll">
				<view v-for="(msgInfo,idx) in chat.messages" :key="idx">
					<!-- {{ msgInfo.content }} -->
					<chat-message-item v-if="idx>=showMinIdx" :headImage="headImage(msgInfo)"
						:showName="showName(msgInfo)" @recall="onRecallMessage" @delete="onDeleteMessage"
						@longPressHead="onLongPressHead(msgInfo)" @download="onDownloadFile" :id="'chat-item-'+idx"
						:msgInfo="msgInfo" :groupMembers="groupMembers" @emitPacket="emitPacket" ref="chatMsgItem">
					</chat-message-item>
					
				</view>
			</scroll-view>
		</view>
		<view v-if="atUserIds.length>0" class="chat-at-bar" @click="openAtBox()">
			<view class="iconfont icon-at">:&nbsp;</view>
			<scroll-view v-if="atUserIds.length>0" class="chat-at-scroll-box" scroll-x="true" scroll-left="120">
				<view class="chat-at-items">
					<view v-for="m in atUserItems" class="chat-at-item">
						<head-image :name="m.aliasName" :url="m.headImage" :size="50"></head-image>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="send-bar">
			<view class="send-text">
				<textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false"
					:placeholder="isReceipt?'[回执消息]':''" :adjust-position="false" @confirm="sendTextMessage()"
					@keyboardheightchange="onKeyboardheightchange" @input="onTextInput" confirm-type="send" confirm-hold
					:hold-keyboard="true"></textarea>
			</view>

			<view v-if="chat.type=='GROUP'" class="iconfont icon-at" @click="openAtBox()"></view>
			<view class="iconfont icon-icon_emoji" @click="switchChatTabBox('emo',true)"></view>
			<view v-if="sendText==''" class="iconfont icon-add" @click="switchChatTabBox('tools',true)">
			</view>
			<button v-if="sendText!=''||atUserIds.length>0" class="btn-send" type="primary"
				@touchend.prevent="sendTextMessage()" size="mini">发送</button>
		</view>

		<view class="chat-tab-bar" v-show="chatTabBox!='none' ||showKeyBoard " :style="{height:`${keyboardHeight}px`}">
			<view v-if="chatTabBox == 'tools'" class="chat-tools">
				<view class="chat-tools-item">
					<image-upload :maxCount="9" sourceType="album" :onBefore="onUploadImageBefore"
						:onSuccess="onUploadImageSuccess" :onError="onUploadImageFail">
						<!-- <view class="tool-icon iconfont icon-picture"></view> -->
						<image src="../../static/img/chat-13.png" style="width: 80rpx; height: 80rpx;"></image>
					</image-upload>
					<view class="tool-name">相册</view>
				</view>
				<view class="chat-tools-item">
					<image-upload sourceType="camera" :onBefore="onUploadImageBefore" :onSuccess="onUploadImageSuccess"
						:onError="onUploadImageFail">
						<!-- <view class="tool-icon iconfont icon-camera"></view> -->
						<image src="../../static/img/chat-14.png" style="width: 80rpx; height: 80rpx;"></image>
					</image-upload>
					<view class="tool-name">拍摄</view>
				</view>
				
				<view class="chat-tools-item">
					<video-upload sourceType="camera" :onBefore="onUploadVideoBefore"
						:onSuccess="onUploadVideoSuccess" :onError="onUploadImageFail">
						<!-- <view class="tool-icon iconfont icon-picture"></view> -->
						<image src="../../static/img/chat-15.png" style="width: 80rpx; height: 80rpx;"></image>
					</video-upload>
					<view class="tool-name">录像</view>
				</view>

				<!-- APP 暂时不支持选择文件 -->
				<!-- <view class="chat-tools-item">
					<file-upload :onBefore="onUploadFileBefore" :onSuccess="onUploadFileSuccess"
						:onError="onUploadFileFail">
						<image src="../../static/img/chat-13.png" style="width: 80rpx; height: 80rpx;"></image>
					</file-upload>
					<view class="tool-name">文件</view>
				</view> -->

				<!-- @click="showTip()" -->
				<view class="chat-tools-item" @click="showAudio()">
					<!-- <audio-upload :onBefore="onUploadAudioBefore" :onSuccess="onUploadAudioSuccess"> -->
					<!-- <view class="tool-icon iconfont icon-microphone"></view> -->
					<image src="../../static/img/chat-16.png" style="width: 80rpx; height: 80rpx;"></image>
					<view class="tool-name">语音输入</view>
					<!-- </audio-upload> -->
				</view>
				<!-- <view v-if="chat.type == 'GROUP'" class="chat-tools-item" @click="switchReceipt()">
					<view class="tool-icon iconfont icon-receipt" :class="isReceipt?'active':''"></view>
					<view class="tool-name">回执消息</view>
				</view> -->
				<!-- showTip() -->
				<!-- <view class="chat-tools-item" @click="jumpPacket">
					<view class="tool-icon iconfont icon-call"></view>
					<view class="tool-name">红包</view>
				</view> -->
			</view>
			<scroll-view v-if="chatTabBox==='emo'" class="chat-emotion" scroll-y="true">
				<view class="emotion-item-list">
					<image class="emotion-item" :title="emoText" :src="$emo.textToPath(emoText)"
						v-for="(emoText, i) in $emo.emoTextList" :key="i" @click="selectEmoji(emoText)" mode="aspectFit"
						lazy-load="true"></image>
				</view>
			</scroll-view>
			<view v-if="showKeyBoard"></view>
		</view>
		<chat-at-box ref="atBox" :ownerId="group.ownerId" :members="groupMembers"
			@complete="onAtComplete"></chat-at-box>

		<audio-upload ref="audio" :onBefore="onUploadAudioBefore" :onSuccess="onUploadAudioSuccess"></audio-upload>


		<uni-popup ref="popup">
			<view class="packet-box">
				<view style="width: 60rpx; height: 60rpx; margin: 0 auto; border-radius: 50%; border: 2rpx solid red;"
					@click="openRedPacker">开</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import navBarMixin from '@/mixin/navBarMixin.js'
	import { openRedPackrApi } from '@/api/api.js'
	import UNI_APP from '@/.env.js'

	export default {
		data() {
			return {
				chat: {},
				friend: {},
				group: {},
				groupMembers: [],
				sendText: "",
				isReceipt: false, // 是否回执消息
				showVoice: false, // 是否显示语音录制弹窗
				scrollMsgIdx: 0, // 滚动条定位为到哪条消息
				chatTabBox: 'none',
				showKeyBoard: false,
				keyboardHeight: 322,
				atUserIds: [],
				showMinIdx: 0, // 下标小于showMinIdx的消息不显示，否则可能很卡

				isRecording: false, // 是否正在录音
				recorderManager: null, // 录音管理器实例
				recordFilePath: '' // 录音文件路径
			}
		},
		mixins: [navBarMixin],
		methods: {
			showTip() {

				uni.showToast({
					title: "暂未支持...",
					icon: "none"
				})
			},
			showAudio() {
				this.$refs.audio.openPopup()
			},

			moveChatToTop() {
				let chatIdx = this.$store.getters.findChatIdx(this.chat);
				this.$store.commit("moveTop", chatIdx);
			},
			switchReceipt() {
				this.isReceipt = !this.isReceipt;
			},
			openAtBox() {
				this.$refs.atBox.init(this.atUserIds);
				this.$refs.atBox.open();
			},
			onAtComplete(atUserIds) {
				this.atUserIds = atUserIds;
			},
			onLongPressHead(msgInfo) {
				if (!msgInfo.selfSend && this.chat.type == "GROUP" && this.atUserIds.indexOf(msgInfo.sendId) < 0) {
					this.atUserIds.push(msgInfo.sendId);
				}
			},
			headImage(msgInfo) {
				// console.log('////////////////',JSON.stringify(msgInfo));
				if (this.chat.type == 'GROUP') {
					let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
					return member ? member.headImage : "";
				} else {
					return msgInfo.selfSend ? this.mine.avatar : this.chat.headImage
				}
			},
			showName(msgInfo) {
				if (this.chat.type == 'GROUP') {
					let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
					return member ? member.aliasName : "";
				} else {
					return msgInfo.selfSend ? this.mine.nickName : this.chat.showName
				}
			},
			sendTextMessage() {
				if (!this.sendText.trim() && this.atUserIds.length == 0) {
					return uni.showToast({
						title: "不能发送空白信息",
						icon: "none"
					});
				}
				let receiptText = this.isReceipt ? "【回执消息】" : "";
				let atText = this.createAtText();
				
				// console.log('this.sendText===',this.sendText)
				let msgInfo = {
					content: receiptText + this.sendText + atText,
					atUserIds: this.atUserIds.length ? this.atUserIds : null,
					receipt: this.isReceipt,
					type: 0
				}
				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				this.sendText = "";
				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					msgInfo.id = id;
					msgInfo.sendTime = new Date().getTime();
					msgInfo.sendId = this.$store.state.userStore.userInfo.id;
					msgInfo.selfSend = true;
					msgInfo.readedCount = 0,
						msgInfo.status = this.$enums.MESSAGE_STATUS.UNSEND;
					this.$store.commit("insertMessage", msgInfo);
					uni.hideKeyboard()
					// 会话置顶
					this.moveChatToTop();
					this.sendText = "";
				}).finally(() => {
					// 滚动到底部
					this.scrollToBottom();
					// 清空@用户列表
					this.atUserIds = [];
					this.isReceipt = false;
				});
			},
			createAtText() {
				let atText = "";
				this.atUserIds.forEach((id) => {
					if (id == -1) {
						atText += ` @全体成员`;
					} else {
						let member = this.groupMembers.find((m) => m.userId == id);
						if (member) {
							atText += ` @${member.aliasName}`;
						}
					}
				})
				return atText;
			},
			fillTargetId(msgInfo, targetId) {
				if (this.chat.type == "GROUP") {
					msgInfo.groupId = targetId;
				} else {
					msgInfo.recvId = targetId;
				}
			},
			scrollToBottom() {
				let size = this.chat.messages.length;
				if (size > 0) {
					this.scrollToMsgIdx(size - 1);
				}
			},
			scrollToMsgIdx(idx) {
				// 如果scrollMsgIdx值没变化，滚动条不会移动
				if (idx == this.scrollMsgIdx && idx > 0) {
					this.$nextTick(() => {
						// 先滚动到上一条
						this.scrollMsgIdx = idx - 1;
						// 再滚动目标位置
						this.scrollToMsgIdx(idx);
					});
					return;
				}
				this.$nextTick(() => {
					this.scrollMsgIdx = idx;
				});

			},
			switchChatTabBox(chatTabBox, hideKeyBoard) {
				this.chatTabBox = chatTabBox;
				if (hideKeyBoard) {
					uni.hideKeyboard();
				}
			},
			selectEmoji(emoText) {
				// const img = `<image src="$emo.textToPath(emoText)"></image>`
				// console.log('img===',this.$emo.textToPath(emoText))
				this.sendText += `#${emoText};`;
			},
			onNavBack() {
				uni.switchTab({
					url: "/pages/chat/chat"
				})
			},
			onKeyboardheightchange(e) {
				if (e.detail.height > 0) {
					this.showKeyBoard = true;
					this.switchChatTabBox('none', false)
					this.keyboardHeight = this.rpxTopx(e.detail.height);
				} else {
					this.showKeyBoard = false;
				}
			},
			onUploadImageBefore(file) {
				let data = {
					originUrl: file.path,
					thumbUrl: file.path
				}
				let msgInfo = {
					id: 0,
					fileId: file.uid,
					sendId: this.mine.id,
					content: JSON.stringify(data),
					sendTime: new Date().getTime(),
					selfSend: true,
					type: this.$enums.MESSAGE_TYPE.IMAGE,
					readedCount: 0,
					loadStatus: "loading",
					status: this.$enums.MESSAGE_STATUS.UNSEND
				}
				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				// 插入消息
				this.$store.commit("insertMessage", msgInfo);
				// 会话置顶
				this.moveChatToTop();
				// 借助file对象保存
				file.msgInfo = msgInfo;
				// 滚到最低部
				this.scrollToBottom();
				return true;
			},
			onUploadVideoBefore(file) {
				console.log('file===',file)
				let data = {
					path: file.tempFilePath
				}
				let msgInfo = {
					id: 0,
					fileId: file.uid,
					sendId: this.mine.id,
					content: JSON.stringify(data),
					sendTime: new Date().getTime(),
					selfSend: true,
					type: this.$enums.MESSAGE_TYPE.VIDEO,
					readedCount: 0,
					loadStatus: "loading",
					status: this.$enums.MESSAGE_STATUS.UNSEND
				}
				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				// 插入消息
				this.$store.commit("insertMessage", msgInfo);
				// 会话置顶
				this.moveChatToTop();
				// 借助file对象保存
				file.msgInfo = msgInfo;
				// 滚到最低部
				this.scrollToBottom();
				return true;
				
			},
			
			onUploadVideoSuccess(file, res) {
				console.log(file)
				console.log('res===',res)
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.content = res.data
				msgInfo.receipt = this.isReceipt
				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					msgInfo.loadStatus = 'ok';
					msgInfo.id = id;
					this.isReceipt = false;
					this.$store.commit("insertMessage", msgInfo);
				})
				
			},
			
			onUploadAudioBefore(file) {
				let data = {
					path: file.path
				}
				let msgInfo = {
					id: 0,
					sendId: this.mine.id,
					content: JSON.stringify(data),
					sendTime: new Date().getTime(),
					selfSend: true,
					type: 3,
					readedCount: 0,
					loadStatus: "loading",
					status: this.$enums.MESSAGE_STATUS.UNSEND
				}

				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				// 插入消息
				this.$store.commit("insertMessage", msgInfo);
				// 会话置顶
				this.moveChatToTop();
				// 借助file对象保存
				file.msgInfo = msgInfo;
				// 滚到最低部
				this.scrollToBottom();
				return true;
			},

			onUploadImageSuccess(file, res) {
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.content = res.data
				msgInfo.receipt = this.isReceipt

				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					msgInfo.loadStatus = 'ok';
					msgInfo.id = id;
					this.isReceipt = false;
					this.$store.commit("insertMessage", msgInfo);
				})
			},

			onUploadImageFail(file, err) {
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.loadStatus = 'fail';
				this.$store.commit("insertMessage", msgInfo);
			},
			onUploadFileBefore(file) {
				let data = {
					name: file.name,
					size: file.size,
					url: file.path
				}
				let msgInfo = {
					id: 0,
					sendId: this.mine.id,
					content: JSON.stringify(data),
					sendTime: new Date().getTime(),
					selfSend: true,
					type: this.$enums.MESSAGE_TYPE.FILE,
					readedCount: 0,
					loadStatus: "loading",
					status: this.$enums.MESSAGE_STATUS.UNSEND
				}
				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				// 插入消息
				this.$store.commit("insertMessage", msgInfo);
				// 会话置顶
				this.moveChatToTop();
				// 借助file对象保存
				file.msgInfo = msgInfo;
				// 滚到最低部
				this.scrollToBottom();
				return true;
			},
			onUploadFileSuccess(file, res) {
				let data = {
					name: file.name,
					size: file.size,
					url: res.data
				}
				
				
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.content = JSON.stringify(data);
				msgInfo.receipt = this.isReceipt
				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					msgInfo.loadStatus = 'ok';
					msgInfo.id = id;
					this.isReceipt = false;
					this.$store.commit("insertMessage", msgInfo);
				})
			},
			onUploadAudioSuccess(file, res) {
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.content = res.data
				msgInfo.receipt = this.isReceipt

				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					console.log('id=====xxx', id)
					this.$refs.audio.closePopup()
					msgInfo.loadStatus = 'ok'
					msgInfo.id = id
					this.isReceipt = false;
					this.$store.commit("insertMessage", msgInfo);

				})
			},


			onUploadFileFail(file, res) {
				let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
				msgInfo.loadStatus = 'fail';
				this.$store.commit("insertMessage", msgInfo);
			},
			onDeleteMessage(msgInfo) {
				uni.showModal({
					title: '删除消息',
					content: '确认删除消息?',
					success: (res) => {
						if (!res.cancel) {
							this.$store.commit("deleteMessage", msgInfo);
							uni.showToast({
								title: "删除成功",
								icon: "none"
							})
						}
					}
				})
			},
			onRecallMessage(msgInfo) {
				uni.showModal({
					title: '撤回消息',
					content: '确认撤回消息?',
					success: (res) => {
						if (!res.cancel) {
							let url = `/message/${this.chat.type.toLowerCase()}/recall/${msgInfo.id}`
							this.$http({
								url: url,
								method: 'DELETE'
							}).then(() => {
								msgInfo = JSON.parse(JSON.stringify(msgInfo));
								msgInfo.type = this.$enums.MESSAGE_TYPE.RECALL;
								msgInfo.content = '你撤回了一条消息';
								msgInfo.status = this.$enums.MESSAGE_STATUS.RECALL;
								this.$store.commit("insertMessage", msgInfo);
							})
						}
					}
				})
			},
			onDownloadFile(msgInfo) {
				let url = JSON.parse(msgInfo.content).url;
				uni.downloadFile({
					url: url,
					success(res) {
						if (res.statusCode === 200) {
							var filePath = encodeURI(res.tempFilePath);
							uni.openDocument({
								filePath: filePath,
								showMenu: true
							});
						}
					},
					fail(e) {
						console.log(e);
						uni.showToast({
							title: "文件下载失败",
							icon: "none"
						})
					}
				});
			},
			onScrollToTop() {
				// #ifdef MP
				// 防止滚动条定格在顶部，不能一直往上滚
				this.scrollToMsgIdx(this.showMinIdx);
				// #endif
				// 多展示10条信息
				this.showMinIdx = this.showMinIdx > 10 ? this.showMinIdx - 10 : 0;
			},
			onShowMore() {
				if (this.chat.type == "GROUP") {
					uni.navigateTo({
						url: "/pages/group/group-info?id=" + this.group.id
					})
				} else {
					uni.navigateTo({
						url: "/pages/common/user-info?id=" + this.friend.id
					})
				}
			},
			onTextInput(e) {
				let idx = e.detail.cursor - 1;
				if (this.chat.type == 'GROUP' && e.detail.value[idx] == '@') {
					this.openAtBox();
					let sendText = e.detail.value.replace("@", '');
					this.$nextTick(() => {
						this.sendText = sendText;
					})
				}
			},
			loadReaded(fId) {
				this.$http({
					url: `/message/private/maxReadedId?friendId=${fId}`,
					method: 'get'
				}).then((id) => {
					this.$store.commit("readedMessage", {
						friendId: fId,
						maxId: id
					});
				});
			},
			readedMessage() {
				if (this.chat.type == "GROUP") {
					var url = `/message/group/readed?groupId=${this.chat.targetId}`
				} else {
					url = `/message/private/readed?friendId=${this.chat.targetId}`
				}
				this.$http({
					url: url,
					method: 'PUT'
				}).then(() => {
					this.$store.commit("resetUnreadCount", this.chat)
					this.scrollToBottom();
				})
			},
			loadGroup(groupId) {
				this.$http({
					url: `/group/find/${groupId}`,
					method: 'GET'
				}).then((group) => {
					this.group = group;
					this.$store.commit("updateChatFromGroup", group);
					this.$store.commit("updateGroup", group);

				});

				this.$http({
					url: `/group/members/${groupId}`,
					method: 'get'
				}).then((groupMembers) => {
					this.groupMembers = groupMembers;
					let list = this.groupMembers.filter(item => {
						return item.quit == true
					})
					let result = list.map(item => item.userId).join(',');
					console.log('**************************',result);
					uni.setStorageSync('blackUser',result)
				});
			},
			loadFriend(friendId) {
				// 获取对方最新信息
				this.$http({
					url: `/user/getUserById?id=${friendId}`,
					method: 'GET'
				}).then((friend) => {
					this.friend = friend;
					this.$store.commit("updateChatFromFriend", friend);
					this.$store.commit("updateFriend", friend);
				})
			},
			rpxTopx(rpx) {
				// px转换成rpx
				let info = uni.getSystemInfoSync()
				let px = info.windowWidth * rpx / 750;
				return Math.floor(rpx);
			},

			jumpPacket() {
				uni.navigateTo({
					url: '/pages/chat/packet?chatIdx=' + this.chatIdx
				})
			},
			emitPacket(e) {
				console.log('红包id===', e)
				this.redPacketId = e
				this.$refs.popup.open()
			},
			
			handleScroll() {
				// console.log(this.$refs.chatMsgItem)
				this.$refs.chatMsgItem.forEach(item => {
					item.menu.show = false
				})
			},
			
			// 开红包
			async openRedPacker() {
				const res = await openRedPackrApi({ id: this.redPacketId })
				if (!res.code) {
					let msgInfo = {
						id: 0,
						sendId: this.mine.id,
						sendTime: new Date().getTime(),
						content: '领取红包',
						selfSend: true,
						type: 13,
						readedCount: 0,
						loadStatus: "loading",
						status: this.$enums.MESSAGE_STATUS.UNSEND
					}

					// console.log('123====')

					// 填充对方id
					this.fillTargetId(msgInfo, this.chat.targetId);
					// 插入消息
					this.$store.commit("insertMessage", msgInfo);
					// 会话置顶
					this.moveChatToTop();
					// 借助file对象保存
					// file.msgInfo = msgInfo;
					// 滚到最低部
					// this.scrollToBottom();

					this.$http({
						url: this.messageAction,
						method: 'POST',
						data: msgInfo
					}).then((id) => {
						msgInfo.loadStatus = 'ok';
						msgInfo.id = id;
						this.isReceipt = false;
						this.$store.commit('insertMessage', msgInfo)
					})
				}
			}
		},
		computed: {
			mine() {
				return this.$store.state.userStore.userInfo;
			},
			title() {
				if (!this.chat) {
					return "";
				}
				let title = this.chat.showName;
				if (this.chat.type == "GROUP") {
					let size = this.groupMembers.filter(m => !m.quit).length;
					title += `(${size})`;
				}
				return title;
			},
			messageAction() {
				return `/message/${this.chat.type.toLowerCase()}/send`;
			},
			messageSize() {
				if (!this.chat || !this.chat.messages) {
					return 0;
				}
				return this.chat.messages.length;
			},
			unreadCount() {
				return this.chat.unreadCount;
			},
			atUserItems() {
				let atUsers = [];
				this.atUserIds.forEach((id) => {
					if (id == -1) {
						atUsers.push({
							id: -1,
							aliasName: "全体成员"
						})
						return;
					}
					let member = this.groupMembers.find((m) => m.userId == id);
					if (member) {
						atUsers.push(member);
					}
				})
				return atUsers;
			}
		},
		watch: {
			messageSize: function(newSize, oldSize) {
				// 接收到消息时滚动到底部
				if (newSize > oldSize) {
					this.scrollToBottom();
				}
			},
			unreadCount: {
				handler(newCount, oldCount) {
					if (newCount > 0) {
						// 消息已读
						this.readedMessage()
					}
				}
			}
		},
		onLoad(options) {
			
			this.chat = this.$store.state.chatStore.chats[options.chatIdx];
			// this.loadGroup(this.$store.state.chatStore.chats[options.chatIdx].targetId);
			let yuanchat = this.$store.state.chatStore.chats[options.chatIdx].messages
			var shaichat
			// let aaaa
			
			this.$http({
				url: `/group/members/${this.$store.state.chatStore.chats[options.chatIdx].targetId}`,
				method: 'get'
			}).then((groupMembers) => {
				this.groupMembers = groupMembers;
				let list = this.groupMembers.filter(item => {
					return item.quit == true
				})
				let result = list.map(item => item.userId).join(',');
				console.log('**************************',result);
				uni.setStorageSync('blackUser',result)
				// let aaaa = yuanchat.filter(item => {
				// 	return  result.includes(item.sendId)
				// })
				let bbbbb = []
				for (var i = 0; i < yuanchat.length; i++) {
					console.log(result.includes(yuanchat[i].sendId));
					if(result.includes(yuanchat[i].sendId)) {
						
					}else {
						bbbbb.push(yuanchat[i])
					}
				}
				console.log(bbbbb);
				console.log(JSON.parse(JSON.stringify(bbbbb)));
				// console.log(aaaa);
				// console.log(JSON.parse(JSON.stringify(aaaa)));
				this.chat.messages = bbbbb
			});
			
			this.chatIdx = options.chatIdx

			// 聊天数据
			console.log('options===', options)
			// console.log(' this.$store.state.chatStore.chats===', this.$store.state.chatStore.chats)

			
			console.log('已删用户====', uni.getStorageSync('blackUser'))
			console.log('聊天数据====', this.chat)
			console.log('聊天数据====', JSON.parse(JSON.stringify(this.chat)))

			// 红包的
			if (options.packet) {
				const packet = JSON.parse(options.packet)
				// console.log('packet===',packet)

				let msgInfo = {
					id: 0,
					sendId: this.mine.id,
					sendTime: new Date().getTime(),
					content: options.packet,
					selfSend: true,
					type: 5,
					readedCount: 0,
					loadStatus: "loading",
					status: this.$enums.MESSAGE_STATUS.UNSEND
				}

				console.log('123====')

				// 填充对方id
				this.fillTargetId(msgInfo, this.chat.targetId);
				// 插入消息
				this.$store.commit("insertMessage", msgInfo);
				// 会话置顶
				this.moveChatToTop();
				// 借助file对象保存
				// file.msgInfo = msgInfo;
				// 滚到最低部
				this.scrollToBottom();



				this.$http({
					url: this.messageAction,
					method: 'POST',
					data: msgInfo
				}).then((id) => {
					msgInfo.loadStatus = 'ok';
					msgInfo.id = id;
					this.isReceipt = false;
					this.$store.commit('insertMessage', msgInfo)
				})
				// return true;

			}

			// 初始状态只显示30条消息
			let size = this.chat.messages.length;
			this.showMinIdx = size > 30 ? size - 30 : 0;
			// 激活当前会话
			this.$store.commit("activeChat", options.chatIdx);
			// 页面滚到底部
			this.scrollToBottom();
			// 消息已读
			this.readedMessage()
			// 加载好友或群聊信息
			if (this.chat.type == "GROUP") {
				this.loadGroup(this.chat.targetId);
			} else {
				this.loadFriend(this.chat.targetId);
				this.loadReaded(this.chat.targetId)
			}
			// 复位回执消息
			this.isReceipt = false;
		},
		onUnload() {
			this.$store.commit("activeChat", -1);
		}
	}
</script>

<style lang="scss" scoped>
	.chat-box {
		position: relative;
		overflow: hidden;
		border: #dddddd solid 1px;
		display: flex;
		flex-direction: column;

		.header {
			// position: fixed;
			// top: 0;
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			// height: 96rpx;
			height: 48px;
			padding: 5px;
			background-color: white;
			line-height: 50px;
			font-size: 40rpx;
			font-weight: 600;
			// border: #dddddd solid 1px;


			.btn-side {
				position: absolute;
				line-height: 60rpx;
				font-size: 28rpx;
				cursor: pointer;

				&.left {
					left: 30rpx;

					image {
						width: 20rpx;
						height: 37rpx;
					}
				}

				&.right {
					right: 30rpx;

					image {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}


		.chat-msg {
			flex: 1;
			padding: 0;
			// border: #dddddd solid 1px;
			overflow: hidden;
			position: relative;
			background-color: #f8f8f8;

			.scroll-box {
				height: 100%;
			}
		}

		.chat-at-bar {
			display: flex;
			align-items: center;
			padding: 0 10rpx;
			border: #dddddd solid 1px;

			.icon-at {
				font-size: 35rpx;
				color: darkblue;
				font-weight: 600;
			}

			.chat-at-scroll-box {
				flex: 1;
				width: 80%;

				.chat-at-items {
					display: flex;
					align-items: center;
					height: 70rpx;

					.chat-at-item {
						padding: 0 3rpx;
					}
				}
			}

		}

		.send-bar {
			// position: fixed;
			// bottom: 0;
			// width: 100%;
			// box-sizing: border-box;
			display: flex;
			align-items: center;
			padding: 10rpx;
			margin-bottom: 10rpx;
			// border: #dddddd solid 1px;
			background-color: white;

			.iconfont {
				font-size: 60rpx;
				margin: 3rpx;
			}

			.send-text {
				flex: 1;
				background-color: #f8f8f8 !important;
				overflow: auto;
				padding: 20rpx;
				background-color: #fff;
				border-radius: 20rpx;
				max-height: 300rpx;
				min-height: 85rpx;
				font-size: 30rpx;
				box-sizing: border-box;

				.send-text-area {
					width: 100%;
				}
			}

			.btn-send {
				margin: 5rpx;
			}
		}


		.chat-tab-bar {
			height: 500rpx;
			padding: 20rpx;
			background-color: #fff;

			.chat-tools {
				display: flex;
				flex-wrap: wrap;

				.chat-tools-item {
					width: 140rpx;
					padding: 16rpx;
					display: flex;
					flex-direction: column;
					align-items: center;

					.tool-icon {
						padding: 18rpx;
						font-size: 80rpx;
						background-color: white;
						border-radius: 20%;

						&.active {
							background-color: #ddd;
						}
					}

					.tool-name {
						height: 60rpx;
						line-height: 60rpx;
						font-size: 25rpx;
					}
				}
			}

			.chat-emotion {
				height: 100%;

				.emotion-item-list {
					display: flex;
					flex-wrap: wrap;

					.emotion-item {
						width: 40px;
						height: 40px;
						text-align: center;
						cursor: pointer;
						padding: 6px;
					}
				}
			}

		}
	}

	.packet-box {
		width: 600rpx;
		height: 300rpx;
		background: #fff;
	}
</style>